﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Linq;
using System.Data.SqlClient;
using System.Text;
using IntraVision.Web;
using IntraVision.Web.Toolkit;
using IntraVision.Web.Security;
using Model;

[AllowedRole("Finance", "NotarialActionReport")]
public class NotarialActionReportController : ReportController<NotarialServiceDataContext>
{
    DateTime _StartDate;
    DateTime _EndDate;
    public NotarialActionReportController(): base()
    {
        /* первичный вариант стартовых дат
        _StartDate = DateTime.Now.AddDays(-7);
        _EndDate = DateTime.Now;
        
        _StartDate = new DateTime(DateTime.Now.Year, DateTime.Now.AddMonths(-1).Month, 1);
        _EndDate = DateTime.Now.AddDays(-DateTime.Now.Day);
        */
        _StartDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
        _EndDate = new DateTime(DateTime.Now.Year, DateTime.Now.AddMonths(1).Month, 1).AddDays(-1);



        _ColumnsCaptions = new string[10] { "Название", "Н Г.п.+ПИТР", "Н Коп.+ПИТР", "Н Ксер.", "Н Всего", "БН Г.п.+ПИТР", "БН Коп.+ПИТР", "БН Ксер.", "БН Всего", "Итого Н+БН" };
    }

    public override void Default()
    {
        InitDatesFromRequest();

        ViewData["months"] = GetMonthList();
        ViewData["years"] = GetYearsList(DateTime.Now.Year, 10);

        base.Default();
    }

    private void InitDatesFromRequest()
    {
        DateTime bufDateTime;
        if (!String.IsNullOrEmpty(Request["startdate"]) && DateTime.TryParse(Request["startdate"], out bufDateTime))
        {
            _StartDate = bufDateTime;
            ViewData["currentYear"] = _StartDate.Year;
            ViewData["currentMonth"] = _StartDate.Month;
        }
        if (!String.IsNullOrEmpty(Request["enddate"]) && DateTime.TryParse(Request["enddate"], out bufDateTime))
            _EndDate = bufDateTime;  
    }
    
    protected override void InitParams(NotarialServiceDataContext dc)
    {
        //подкорректируем диапазон дат, чтобы учесть крайние границы времени
        _StartDate = _StartDate.AddHours(-_StartDate.Hour);
        _StartDate = _StartDate.AddMinutes(-_StartDate.Minute);
        _EndDate = _EndDate.AddHours(-_EndDate.Hour + 23);
        _EndDate = _EndDate.AddMinutes(-_EndDate.Minute + 59);

        //добавим параметры
        _CommandParams.Clear();
        _CommandParams.Add("@StartDate", _StartDate);
        _CommandParams.Add("@EndDate", _EndDate);        
    }

    protected override void DataReceived(DataSet ds)
    {
        base.DataReceived(ds);

        ViewData["startdate"] = _StartDate;
        ViewData["enddate"] = _EndDate;
    }
}
